package com.yl.immodule.fragment

import android.view.View
import androidx.fragment.app.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.camera.base.v.BaseFragment
import com.camera.routemodule.RouterPath
import com.yl.immodule.R
import com.yl.immodule.databinding.FragmentAccStateBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.path.acc_fragment_state)
class AccStateFragment : BaseFragment<FragmentAccStateBinding>() {
    private val viewModel: AccstateModel by viewModels()
    
    override fun loadData(isRefresh: Boolean) {
        // 设置viewModel到databinding
        mBinding.viewModel = viewModel
        // 观察连接状态变化，手动设置状态圆点
        observeConnectionState()
    }

    override fun initView() {
        // 初始化状态圆点为离线状态
        updateStatusIndicator(AccstateModel.ConnectionState.DISCONNECTED)
    }
    
    /**
     * 观察连接状态变化
     */
    private fun observeConnectionState() {
        viewModel.connectionState.observe(this) { connectionState ->
            updateStatusIndicator(connectionState)
        }
    }
    
    /**
     * 更新状态指示圆点
     * @param connectionState 连接状态
     */
    private fun updateStatusIndicator(connectionState: AccstateModel.ConnectionState) {
        val drawableRes = when (connectionState) {
            AccstateModel.ConnectionState.CONNECTED -> R.drawable.status_indicator_online      // 绿色
            AccstateModel.ConnectionState.CONNECTING -> R.drawable.status_indicator_connecting // 黄色
            AccstateModel.ConnectionState.DISCONNECTED -> R.drawable.status_indicator_offline  // 灰色
        }
        
        mBinding.statusIndicator.setBackgroundResource(drawableRes)
        mBinding.statusIndicator.visibility = View.VISIBLE
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_acc_state
    }
}